<?php
//controller nay dung de tim kiem, khong can Model
class TimkiemcvsController extends AppController {

	var $name = 'Timkiemcvs';
    var $uses=array('Khtuan','Thuocdv','Donvi','Nvngay','Nvchithi','Kehoach','Thoigian','Tempnhiemvunv','Zdv');
    var $helpers=array('Ctable');
    var $paginate=array('limit'=>100);
    function search(){
   	    $thangs=$this->Thoigian->find('list',array('conditions'=>array('thoigian_id > ?'=>array(0))));
        $donvis=$this->Donvi->find('list',array(
                'fields'=>array('Donvi.id','Donvi.tendv'),'recursive'=>-1));
        $chons=array(1=>'Kế hoạch tuần',2=>'Cv chỉ thị',3=>'CV ngày');
        $this->set(compact('kh','donvis','khtuans','thangs','chons','khcha','dv'));
        
        
    }
    function excelth($khcon,$kcha){
                
            $kh=$this->Kehoach->find('first',array(
                          'conditions'=>array('id'=>$khcon),'recursive'=>-1));
            
            $tendv=$this->Zdv->find('first',array(
                'conditions'=>array('donvi_id'=>$kh['Kehoach'] ['donvi_id']),
                'fields'=>array('tendv','tendvcha'),'recursive'=>-1));
            
            $this->hiennvngay($khcon);
                        
            $khcha=$this->Kehoach->find('first',array(
                           'conditions'=>array('id'=>$kcha),'recursive'=>-1));
            $this->hiennvchithi($kcha);
            $this->layout='';
            $this->set(compact('dv','kh','khcha','tendv'));
                    
    }
    function indexexcel($kehoach=1,$idnhanvien=null) {
        
        
        
        $this->Nvchithi->recursive = 0;
        
        $kh=$this->Nvchithi->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach),'recursive'=>0));
        
        $tendv=$this->Nvchithi->query("select tendvcha, tendv from zdvs where
                    donvi_id='".$kh['Kehoach'] ['donvi_id']."'");
        $this->set(compact('tendv'));
        
        $dagiao=1;
        //neu khong co tham so thu 2 thi la bang Ke hoach cong viec danh cho ca don vi
        if(empty($idnhanvien)){
		    $nvchithis= $this->Nvchithi->find('all',array(
                    'conditions'=>array('Nvchithi.kehoach_id'=>$kehoach)));
            $this->set(compact('nvchithis'));
            
     
            
        }else{
            $nvchithis= $this->Nvchithi->find('all',array(
                    'conditions'=>array(
                        'Nvchithi.kehoach_id'=>$kehoach,'nhanvien_id'=>$idnhanvien)));
            $this->set(compact('nvchithis'));
            
            if(count($nvchithis)>0){
                if($nvchithis[0]['Nvchithi']['dagiao']!=1&&$nvchithis[0]['Nvchithi']['dagiao']!=2){
                $this->Session->setFlash(__('Công việc ở kế hoạch này chưa được giao', true));
            //    $this->redirect(array('controller'=>'kehoaches','action'=>'indexnv',$kh['Kehoach']['donvi_id']));
                }
            }
        }
        
        
        $this->layout='';  
        $this->set(compact('kehoach','kh','idnhanvien','dagiao','tendv'));
	}
    function excel($kehoach=1,$idnhanvien=null){
       
       $kh=$this->Nvngay->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach)));
       
       $tendv=$this->Zdv->find('first',array(
                'conditions'=>array('donvi_id'=>$kh['Kehoach'] ['donvi_id']),
                'fields'=>array('tendv','tendvcha'),'recursive'=>-1));
       $this->set(compact('kh','tendv'));
        $this->Nvngay->recursive = -1;
        if(!empty($idnhanvien)){
            $dulieu=$this->Nvngay->find('all',array(
                'conditions'=>array('kehoach_id'=>$kehoach,'nhanvien_id'=>$idnhanvien),
                'order'=>array('nhanvien_id'=>'asc','id'=>'asc','ngayth'=>'asc')));
                    
            $nhanviens=$this->Tempnhiemvunv->find('all',array(
                'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                'order'=>array('nhanvien_id'=>'asc'),
                'conditions'=>array('kehoach_id'=>$kehoach,'nhanvien_id'=>$idnhanvien)));
     //   print_r($dulieu);
        }  
        else{
            $dulieu=$this->Nvngay->find('all',array('conditions'=>array('kehoach_id'=>$kehoach),
                    'order'=>array('nhanvien_id'=>'asc','id'=>'asc','ngayth'=>'asc')));
                    
            $nhanviens=$this->Tempnhiemvunv->find('all',array(
                'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                'order'=>array('nhanvien_id'=>'asc'),
                'conditions'=>array('kehoach_id'=>$kehoach,
                        'Tempnhiemvunv.donvi_id'=>$kh ['Kehoach'] ['donvi_id'])));
        }
        
            $this->set(compact('dulieu','nhanviens','kehoach','idnhanvien'));
        $this->layout='';
        
        
    }
   
    function kq(){
        $this->layout='ajax';
        if(!empty($this->data['Timkiemcv'] ['chon'])){
            $dv=$this->Donvi->find('first',array('conditions'=>
                            array('id'=>$this->data['Timkiemcv']['donvi_id']),'
                            fields'=>'tendv'));      
            foreach($this->data['Timkiemcv'] ['chon'] as $kq){
                $kh=$this->Kehoach->find('first',array(
                          'conditions'=>array('Kehoach.thoigian_id'=>$this->data['Timkiemcv']['tuan'],
                 
                                            'donvi_id'=>$this->data['Timkiemcv']['donvi_id'])));
            
                if($kq==1)
                    $this->hienkhuan($kh['Kehoach'] ['id']);
                elseif($kq==3){
                       
                        if(empty($kh))
         		         	$this->Session->setFlash(__('Thời gian này đơn vị chưa lập kế hoạch tuần', true));
                   
                        else
                            $this->hiennvngay($kh['Kehoach'] ['id']);
                }
                else{//IF KQ=2
                
                        $thcha=$this->Thoigian->find('list',array(
                            'conditions'=>array(
                                    'Thoigian.id'=>$this->data['Timkiemcv']['tuan']),
                                    'fields'=>array('thoigian_id','thoigian_id')));
                        
                        $khcha=$this->Kehoach->find('first',array(
                            'conditions'=>array(
                                            'Kehoach.thoigian_id'=>$thcha,
                                            'donvi_id'=>$this->data['Timkiemcv']['donvi_id'])));       
                        if(empty($khcha)){
         		            $this->Session->setFlash(__('Thời gian này đơn vị chưa lập Kế hoạch cho công văn chỉ thị', true));
                        
                        }else{
                            $this->hiennvchithi($khcha['Kehoach'] ['id']);
                        }
                    
                }
            }
                    
            
        }
      //  $nvchithis=array();
        //$dulieu=array();
        $this->set(compact('kh','donvis','khtuans','khcha','dv','nvchithis','dulieu'));
        
    }
    function hienkhuan($kehoach){
        if(!empty($this->data)){
            $dv1=$this->data['Timkiemcv']['donvi_id'];
            $this->Thuocdv->recursive = -1;
            
           
            $dlcha=$this->Thuocdv->find('list',array(
                        'conditions'=>array('donvi'=>$dv1),
                        'fields'=>array('donvi_id','donvi_id')));
            $dl=$this->Thuocdv->find('list',array(
                        'conditions'=>array('donvi_id'=>$dlcha),
                        'fields'=>array('donvi','donvi')));
          
            $khtuans=$this->Khtuan->find('all',array(
                            'conditions'=>array('Khtuan.donvi_id'=>$dl,'kehoach_id'=>$kehoach),
                            'recursive'=>0,
                            'order'=>array('ngay'=>'asc')));
           // debug($dl);
            
        }
        //print_r('du lieu'.$this);
        $this->set(compact('kehoach','khtuans'));
    }
    
    function hiennvngay($kehoach=1){
        $kh=$this->Nvngay->Kehoach->find('first',array('conditions'=>array('Kehoach.id'=>$kehoach)));
       
        $this->Nvngay->recursive = -1;
        $dulieu=$this->Nvngay->find('all',array('conditions'=>array('kehoach_id'=>$kehoach),
                    'order'=>array('nhanvien_id'=>'asc','id'=>'asc','ngayth'=>'asc')));
                    
        $nhanviens=$this->Tempnhiemvunv->find('all',array(
                'fields'=>array('id','Nhanvien.manv','Nhanvien.tennv','Tempnhiemvunv.kehoach_id',
                                        'Tempnhiemvunv.nhanvien_id'),
                'order'=>array('nhanvien_id'=>'asc'),
                'conditions'=>array('kehoach_id'=>$kehoach,'Tempnhiemvunv.donvi_id'=>$kh ['Kehoach'] ['donvi_id'])));
      //  print_r($nhanviens);
      //  print_r($dulieu);
        
        $this->set(compact('dulieu','nhanviens','kehoach'));
        
        
    }
    
     function hiennvchithi($kehoach=1) {
        $this->Nvchithi->recursive = 0;
    
        //neu khong co tham so thu 2 thi la bang Ke hoach cong viec danh cho ca don vi
   	    $nvchithis= $this->paginate('Nvchithi',array('Nvchithi.kehoach_id'=>$kehoach));
    
        $this->set(compact('kehoach','nvchithis'));
	}
    
    
}
?>